package com.maomao.maomaochong_i.zhuye.ReceiveFactory;

import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

//
import com.maomao.maomaochong_i.R;
import com.maomao.maomaochong_i.global.Show;
import com.maomao.maomaochong_i.zhuye.Adaper.ViewPagerAdaper;
import com.maomao.maomaochong_i.zhuye.ZhuYeView;

import java.util.ArrayList;
import java.util.List;

public class DingLang {
    private LayoutInflater layoutInflater;
    private ViewGroup container;
    private View view;
    static LayoutInflater getLayout;
    static LinearLayout getLiner;

    public DingLang(LayoutInflater layoutInflater, ViewGroup container) {
        this.layoutInflater = layoutInflater;
        this.container = container;
        getLayout = layoutInflater;
    }

    public View getView(){
        view = layoutInflater.inflate(R.layout.shouye,container,false);
        new YeJianZai().start();
        return view;
    }


    private class YeJianZai{
        private RadioGroup radioGroup;
        private final RadioButton TAB[] = new RadioButton[9];
        private List<View> viewList;
        private final int RADIOBUTTONID[] = {0,R.id.radioButton1,R.id.radioButton2,
                R.id.radioButton3, R.id.radioButton4,R.id.radioButton5};
        private ViewPager pager;
        private void start(){
            initViews();
            radioLister();
        }


        private void initViews() {
            pager=(ViewPager) view.findViewById(R.id.vp);
            viewList=getData();
            ViewPagerAdaper adapter = new ViewPagerAdaper(viewList);
            pager.setAdapter(adapter);
            pager.setOnPageChangeListener(new YeHuaDong());
        }
        private final Integer BUJU[] = {R.layout.newhand,R.layout.secondphone,
                R.layout.phone_accessories,R.layout.interestin_product};
        private List<View> getData(){
            viewList=new ArrayList<>();
            LayoutInflater mInflater= LayoutInflater.from(layoutInflater.getContext());
            LinearLayout layout = (LinearLayout) view.findViewById(R.id.layoutceshi);
            View tab = new ZhuYeView(layoutInflater,layout,pager).getview();
            viewList.add(tab);
            for (Integer buju:BUJU){
                tab = mInflater.inflate(buju,null);
                viewList.add(tab);
            }
            return viewList;
        }//首页页面的添加

        private class YeHuaDong implements ViewPager.OnPageChangeListener {

            //当滑动状态改变时调用
            public void onPageScrollStateChanged(int state) {

            }

            //当前页面被滑动时调用
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            //当新的页面被选中时调用
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        TAB[1].setChecked(true);
                        break;
                    case 1:
                        getLiner = (LinearLayout) view.findViewById(R.id.newxianshiceshi);
                        ShowFactory.makeShow(Show.NEWPHONE).show();
                        TAB[2].setChecked(true);
                        break;
                    case 2:
                        getLiner = (LinearLayout) view.findViewById(R.id.secondxianshiceshi);
                        ShowFactory.makeShow(Show.SENCONEDPHOE).show();
                        TAB[3].setChecked(true);
                        break;
                    case 3:
                        getLiner = (LinearLayout) view.findViewById(R.id.phoneaccessories);
                        ShowFactory.makeShow(Show.PHONEACCESSORIES).show();
                        TAB[4].setChecked(true);
                        break;
                    case 4:
                        getLiner = (LinearLayout) view.findViewById(R.id.interestin);
                        ShowFactory.makeShow(Show.INTERESSTION).show();
                        TAB[5].setChecked(true);
                        break;
                }
            }
        }//首页需要动态添加的事件



        private void radioLister(){
            getRadioID();
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    switch (checkedId) {
                        case R.id.radioButton1:
                            pager.setCurrentItem(0);//选择某一页
                            break;
                        case R.id.radioButton2:
                            pager.setCurrentItem(1);
                            break;
                        case R.id.radioButton3:
                            pager.setCurrentItem(2);
                            break;
                        case R.id.radioButton4:
                            pager.setCurrentItem(3);
                            break;
                    }
                }
            });
        }

        private void getRadioID(){
            radioGroup=(RadioGroup) view.findViewById(R.id.rg);
            for(int i = 1;i<RADIOBUTTONID.length;i++){
                TAB[i] = (RadioButton) view.findViewById(RADIOBUTTONID[i]);
            }
        }


    }


}
